package ATM案例;

import com.sun.javaws.security.AppContextUtil;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class ATM登陆界面 {
    //ATM界面
    public static void main(String[] args) {
        Scanner sr=new Scanner(System.in);
        ArrayList<Acount> Acounts=new ArrayList<>();
        while (true) {
            System.out.println("========欢迎来到ATM系统==========");
            System.out.println("1.登录账户");
            System.out.println("2.创建账户");
            System.out.println("输入您的操作");
            int command=sr.nextInt();
            switch (command){
                case 1:
                    dneglu(Acounts,sr);
                    break;
                case 2:
                    createAcount(Acounts,sr);
                    break;
                default:
                    System.out.println("您输入的操作命令有问题");
                    break;
            }
        }
    }
    //注册
    public static void createAcount(ArrayList<Acount> Acounts,Scanner sr){
        System.out.println("===========系统开户界面============");
        Acount A=new Acount();
        System.out.println("输入您的账户用户名");
        String username= sr.next();
        A.setUsername(username);
        while (true) {
            System.out.println("输入您的密码");
            String p1= sr.next();
            System.out.println("确认您的密码");
            String p2=sr.next();
            if(p1.equals(p2)){
                A.setPassWorld(p1);
                break;
            }else{
                System.out.println("输入的俩次密码不一致");
            }
        }
        System.out.println("请您输入账号的额度");
        double xiane=sr.nextDouble();
        A.setCountMoney(xiane);
        String kh=creatId(Acounts);
        A.setId(kh);
        Acounts.add(A);
        System.out.println("恭喜您"+username+"先生/女士您开户成功你的卡号："+A.getId()+"请您妥善保管");
    }
    //生成卡号
    public static String creatId(ArrayList<Acount> Acounts){
        Random sj=new Random();
        while (true) {
            String cardId="";
            for (int i = 0; i < 8; i++) {
                cardId+=sj.nextInt(10);
            }
            if(pdchongfu(Acounts,cardId)==null){
                return cardId;
            }
        }

    }
    //查询账户
    public static Acount pdchongfu(ArrayList<Acount> Acounts,String cardId){
        for (int i = 0; i < Acounts.size(); i++) {
            if(Acounts.get(i).getId().equals(cardId)){
                return Acounts.get(i);
            }
        }
        return null;
    }
    //登录
    public static void dneglu(ArrayList<Acount> Acounts,Scanner sr){
        System.out.println("=========欢迎来到登录界面==========");
        if(Acounts.size()==0){
            System.out.println("对不起，当前系统中，没有一个账户，请先注册再来登录");
            return;
        }
        while (true) {
            System.out.println("输入您的卡号:");
            String kh=sr.next();
            Acount S= pdchongfu(Acounts,kh);
            if(S==null){
                System.out.println("查无此号");
            }else{
                while (true) {
                    System.out.println("输入您的密码");
                    String mm=sr.next();
                    if(S.getPassWorld().equals(mm)){
                        System.out.println("恭喜您"+S.getUsername()+"先生/女士登陆成功您的卡号："+S.getId());
                        caoZuo(S,sr,Acounts);
                        return;
                    }else{
                        System.out.println("对不起您输入的密码有误");
                    }
                }
            }
        }
    }
    //登录后页面
    public static void caoZuo(Acount S,Scanner sr,ArrayList<Acount> Acounts){
        while (true) {
            System.out.println("============系统操作页面============");
            System.out.println("1.查询账户");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            System.out.println("请选择您的操作");
            int command=sr.nextInt();
            switch (command){
                case 1:showAcount(S);
                    break;
                case 2:cunMoney(S,sr);
                    break;
                case 3:quMoney(S,sr);
                    break;
                case 4:zhuanMoney(S,sr,Acounts);
                    break;
                case 5:gaiPassWorld(S,sr);
                    break;
                case 6:
                    System.out.println("退出成功欢迎下次登录");
                    return;
                case 7:deletAcount(Acounts,sr,S);
                    return;
                default:
                    System.out.println("没有该命令");break;
            }
        }
    }

    private static void deletAcount(ArrayList<Acount> Acounts, Scanner sr, Acount S) {
        System.out.println("您真的要注销账号嘛？yes/no");
        String ok=sr.next();
        switch (ok){
            case "yes":Acounts.remove(S);
                System.out.println("销户成功");
                break;
            default:return;
        }
    }


    //查询当前账户信息
    private static void showAcount(Acount S) {
        System.out.println("========当前账户信息========");
        System.out.println("卡号："+S.getId());
        System.out.println("账户："+S.getUsername());
        System.out.println("余额："+S.getMoney());
        System.out.println("额度："+S.getCountMoney());
    }
    //存款
    private static void cunMoney(Acount S, Scanner sr) {
        System.out.println("请输入存款金额");
        double money=sr.nextDouble();
        S.setMoney(money+S.getMoney());
        showAcount(S);
        return;
    }
    //取钱
    private static void quMoney(Acount S, Scanner sr) {
        if(S.getMoney()<100){
            System.out.println("您的余额小于100，先存款");
            return;
        }
        while (true) {
            System.out.println("您的余额为："+S.getMoney());
            System.out.println("请输入您取款金额：");
            double money=sr.nextDouble();
            if(money>S.getCountMoney()){
                System.out.println("对不起超过取款金额");
            }else if(money<S.getMoney()){
                System.out.println("取款成功");
                S.setMoney(S.getMoney()-money);
                showAcount(S);
                return;
            }else{
                System.out.println("对不起您的余额不足你的余额为："+S.getMoney());
            }
        }
    }
    //转账
    private static void zhuanMoney(Acount S, Scanner sr, ArrayList<Acount> Acounts) {
        if(Acounts.size()<2){
            System.out.println("当前系统账户不足俩个账号请去开户");
            return;
        }
        if(S.getMoney()==0){
            System.out.println("对不起穷鬼你的余额无法转账");
            return;
        }
        while (true) {
            System.out.println("输入对方卡号");
            String kh=sr.next();
            Acount B;
            B=pdchongfu(Acounts,kh);
            if(B.getId()==S.getId()){
                System.out.println("您不可给自己转账");
                continue;
            }
            if(B==null){
                System.out.println("查无此账户");
                continue;
            }else{
                System.out.println("请输入["+"*"+B.getUsername().substring(1)+"]的姓氏");
                String firstName=sr.next();
                if(B.getUsername().startsWith(firstName)){
                    System.out.println("输入转账金额");
                    double money=sr.nextDouble();
                    if(money>S.getMoney()){
                        System.out.println("对不起您的余额不足您的余额为："+S.getMoney());
                    }else{
                        S.setMoney(S.getMoney()-money);
                        B.setMoney(B.getMoney()+money);
                        System.out.println("转账成功");
                        System.out.println("您的余额为"+S.getMoney());
                        return;
                    }
                }else{
                    System.out.println("输入错误姓氏");
                }
            }
        }
    }
    //修改密码
    private static void gaiPassWorld(Acount S, Scanner sr) {
        System.out.println("请您输入当前密码");
        String oldMm=sr.next();
        if(oldMm.equals(S.getPassWorld())){
            while (true) {
                System.out.println("请您输入新密码");
                String newMm=sr.next();
                System.out.println("再次输入新密码");
                String newMm2=sr.next();
                if(newMm.equals(newMm2)){
                    S.setPassWorld(newMm);
                    System.out.println("恭喜您成功修改密码");
                    return;
                }else{
                    System.out.println("俩次密码不一致");
                    continue;
                }
            }
        }else{
            System.out.println("密码错误");
        }
    }
}
